const { findByIdAndDelete } = require('../../models/User')

module.exports = app => {
  const express = require('express')
  const router = express.Router()
  const Test = require('../../models/Test')
  const Subject = require('../../models/Subject')
const Answer = require('../../models/Answer')
const User = require('../../models/User')  
const Mytest = require('../../models/Mytest')
const StudentUser = require('../../../serve/models/StudentUser')
const assert = require('http-assert')
  router.get('/home', async(req,res) => {
    const model = await Subject.find().limit(3)
    res.send(model)
  })
  router.get('/subject', async(req,res) => {
    const model = await Subject.find()
    res.send(model)
  })
  router.get('/test/:id', async(req,res) => {
    const model = await Test.find({
      categories : req.params.id
    })
    res.send(model)
  })
  router.get('/test', async(req,res) => {
    const model = await Test.find()
    res.send(model)
  })
 router.get('/questions/:id', async(req,res) => {
   const model = await Test.findById(req.params.id)
   res.send(model) 
 })
 router.post('/answer', async(req,res) => {
  //  const id = await Answer.find({
  //    _id: req.body._id
  //  })
  //  const once = id === []
  //  assert(!once, 405, '只能有一次答题的机会吆')
   
  const model = await Answer.create(req.body)
  res.send(model)
 })
 router.get('/user/:user', async(req,res) => {
   const model = await Answer.find({
    username: req.params.user
   })
   res.send(model)
 })
//  router.post('/mytest/:user', async(req,res) => {
//    const model = await Mytest.create(req.body)
//    console.log(req.body);
//    console.log(model);
//    res.send(model)
//  })
 router.get('/mytest/:user', async(req,res) => {
   const model = await Answer.find({
     username: req.params.user
   }).populate('subject')
   res.send(model)
 })

 router.get('/myquestions/:id', async(req,res) => {
   const model = await Answer.findById(req.params.id)
   res.send(model)
 })

 router.get('/myquestions', async(req,res) => {
   const model = await Answer.find()
   res.send(model)
 })
router.get('/login', async(req,res)=> {
  const model = await StudentUser.find()
  res.send(model)
})

 
app.use('/student/api', router)

const multer = require('multer')
const upload = multer({dest: __dirname + '/../../uploads'})
app.use('/student/api/upload', upload.single('file'),async (req,res) => {
  console.log(2);
  const file = req.file
  file.url = `http://www.lihuamao.top/uploads/${file.filename}`
  res.send(file)
 })

 app.use(async (err,req,res,next) => {
  res.status(err.statusCode || 500).send({
    message: '只能有一次答题的机会吆'
  })
})
}

